﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using Tools;

namespace Punia
{
    [CommandAttribute(true)]
    public partial class end_conversation : BaseForm
    {
        public end_conversation()
            : base()
        {
        }

        public end_conversation(DbContext dbContext)
            : base(dbContext)
        {
            InitializeComponent();

            cbConvers.DataSource = Utility.conversations(dbContext);
            cbConvers.DisplayMember = "Name";
        }

        protected override void Write()
        {
            StringBuilder sb = new StringBuilder();

            sb.AppendLine("USE [" + dbContext.Database + "];");
            sb.AppendLine();
            if (cbConvers.SelectedIndex > 0)
                sb.AppendLine("END CONVERSATION cast('" + cbConvers.Text + "' as uniqueidentifier)");
            else
                sb.AppendLine("END CONVERSATION cast('' as uniqueidentifier)");

            if (tbError.Text != string.Empty)
            {
                sb.AppendLine("WITH ERROR = " + tbError.Text + " DESCRIPTION = '" + tbDesc.Text + "'");
            }

            if (cbClean.Checked)
            {
                sb.AppendLine("WITH CLEANUP");
            }

            tbCommand.Text = sb.ToString();
        }

        private void cbConvers_SelectedIndexChanged(object sender, EventArgs e)
        {
            Write();
        }

        private void cbClean_CheckedChanged(object sender, EventArgs e)
        {
            Write();
        }

        private void tbError_TextChanged(object sender, EventArgs e)
        {
            Write();
        }

        private void tbDesc_TextChanged(object sender, EventArgs e)
        {
            Write();
        }
    }
}